/* 
Country of Women? Repercussions of the Triple Alliance War in Paraguay
Alix-Garcia, Schechter, Caicedo, Zhu											
Date Modified: July, 2022	
*/

* This do file create all Stata figures included in Alix-Garcia et al (2022 JEBO)

*   =====================================
*   Housekeeping
*   =====================================

* -----
drop _all
clear all
set varabbrev on
set memory  900m
set matsize 11000
set maxvar  15000
set more off

* ----- users
global zhu         1
global schechter   0
global alix_garcia 0

if $zhu {
global dropbox     "C:/Users/siyao/Dropbox (Personal)"
}

if $schechter {
global dropbox     "D:/documents/Dropbox"
}

if $alix_garcia{
global dropbox     "C:/Users/alixgarj/Dropbox" 
}

* project folder
global triplealliance_project  "$dropbox/TripleAllianceAncestry"
global tap_replication         "$triplealliance_project/Data replication package/JEBO - July 2022"
global tap_replication_data    "$tap_replication/Dataset for analysis replication"
global tap_replication_output  "$tap_replication/Output"

* current directory
cd "$tap_replication"

* choose the cutoff
global cutoff_km=30 

*   =====================================
*   Figure 1: Timeline and spatial extent of war
*   ===================================== 

* create by Jen in GIS

*   =====================================
*   Figure 2: Municipalities in cross-border analysis
*   ===================================== 

* create by Jen in GIS

*   =====================================
*   Figure 3: Paraguay and Argentina population sex ratio by birth cohort
*   ===================================== 

use "$tap_replication_data/ParArg_18861895Census_SexRatio_CountryLevel_restrict.dta", clear
//use "$triplealliance_data_analyze\ParArg_18861895Census_SexRatio_CountryLevel.dta", clear

graph twoway (connected sex_ratio Born_cohort_end if Country=="Paraguay") (connected sex_ratio Born_cohort_end if Country=="Argentina_Corrientes") (connected sex_ratio Born_cohort_end if Country=="Argentina_Non"), ///
              xline(1864 1870, lcolor(black)) text(3.2 1864.5 "War", place(e)) ///
			  xlabel(1815 1825 1835 1845 1855 1865 1876 1881 1886, valuelabel angle(45) labsize(*.65)) ///
			  ytitle("Sex ratio: female per male") xtitle("Birth cohort") title("") ///
		      legend(label(1 "Paraguay") label(2 "Argentina (Corrientes)") label(3 "Argentina (Non-Corrientes)") col(3) size(vsmall)) ///			  
			  scheme(s2mono) graphregion(color(white)) 		
graph export "$tap_replication_output/Figure3_ParArg_PopSexRatio_CountryLevel.png", replace width(3900)			  
//graph export "$graph_folder\ParArg_DS_Census_Pop18861895_SexRatio_CountryLevel.png", replace width(3900)

*   =====================================
*   Figure 4: Relationship between the distances to march lines and sex ratios, 1886
*   =====================================

use "$tap_replication_data/Paraguay_1886Census_GenderRatio_Poly_V1_Analysis_restrict.dta", clear
//use "$triplealliance_data_analyze\Paraguay_1886Census_GenderRatio_Poly_V1_Analysis.dta", clear

* ------ lowess provides locally weighted scatterplot: female/male
lowess pop_gender_ratio_rev march_1km if born_before_war==1, bwidth(.8) ylabel(0(1)7) ///
	   ytitle("Sex ratio: female per male") xtitle("Km to the nearest march") title("Born before the war") ///
	   note("") graphregion(color(white)) scheme(sj) saving(born_before, replace)       
lowess pop_gender_ratio_rev march_1km if born_before_war==0, bwidth(.8) ylabel(0(1)7) ///
	   ytitle("Sex ratio: female per male") xtitle("Km to the nearest march") title("Born after the war") ///
	   note("") graphregion(color(white)) scheme(sj) saving(born_after, replace)  
graph combine born_before.gph born_after.gph, ///
              row(1) col(2) ycommon graphregion(color(white)) scheme(s2mono)
graph export "$tap_replication_output/Figure4_Paraguay_PopSexRatio_DistMarch.png", replace width(3900)			
//graph export "$graph_folder\Paraguay_DS_Census_Pop1886_SexRatio_DistMarch_Lowess8_FM.png", replace width(3900)

erase born_before.gph
erase born_after.gph

*   =====================================
*   Figure 5: Out-of-wedlock birth rates across Paraguay, Argentina, and Brazil
*   =====================================

use "$tap_replication_data/ThreeCountries_Ancestry_NaturalBirth_PersonLevel_All3Nations_restrict.dta", clear
//use "$triplealliance_data_analyze\ThreeCountries_Ancestry_NaturalBirth_PersonLevel_All3Nations.dta", clear

* draw figures
twoway (line natural cst_year if source == 2) (line natural cst_year if source == 6) (line natural cst_year if source == 10), ///
	    ytitle(Share of out-of-wedlock births) xtitle(Year of baptism) graphregion(color(white)) scheme(s2mono) ///
		legend(label(1 "Paraguay") label(2 "Argentina") label(3 "Brazil") rows(1)) ///
		xline(1864 1870) text(.85 1865  "War", place(e)) 
graph export "$tap_replication_output/Figure5_OutOfWedlock_ParArgBra.png", replace width(3900)		
//graph export "$graph_folder\country_mean_natural_cst_yr_1840.png", replace width(3900)

*   =====================================
*   Figure 6: Out-of-wedlock birth rates across Paraguay and Corrientes, Argentina
*   =====================================

use "$tap_replication_data/ThreeCountries_Ancestry_NaturalBirth_PersonLevel_CompMuni_restrict.dta", clear
//use "$triplealliance_data_analyze\ThreeCountries_Ancestry_NaturalBirth_PersonLevel_CompMuni.dta", clear

* draw figures
twoway (line natural cst_year if ID_0 == 177) (line natural cst_year if ID_0 == 12), ///
		ytitle(Share of out-of-wedlock births) xtitle(Year of baptism) graphregion(color(white)) scheme(sj) ///
		legend(label(1 "Paraguay") label(2 "Argentina") col(2)) ///
		xline(1864 1870) text(.85 1865  "War", place(e)) 
graph export "$tap_replication_output/Figure6_OutOfWedlock_ParArg.png", replace width(3900)
//graph export "$graph_folder\country_mean_natural_cst_yr_rborder_mun1840.png", replace width(3900)

*   =====================================
*   Appendix Figure C.1: Share of out-of-wedlockba baptisms in Paraguaya church records and births in Paraguayan census data
*   =====================================

* ---- At the Municipality Level
use "$tap_replication_data/Paraguay_ComAncestryCesus_IllegitCYear_Muni_restrict.dta", clear 
//use "$triplealliance_data_analyze\Paraguay_ComAncestryCesus_IllegitCYear_Muni.dta", clear 

graph bar (mean) birth_ratio_illegit (mean) birth_ratio_illegitadj_census if m_census==3, ///
		  over(cst_year, label(angle(90) labsize(large))) ///
		  by(Municipality_GIS, title("At the Municipality Level") note("")  ///
		  iscale(0.4) yrescale graphregion(color(white)) legend(off)) ///
		  ylabel(0 0.2 0.4 0.6 0.8, labsize(*.8)) bar(1, color(black*0.5)) bar(2, color(gray*0.3)) blabel(total, position(base) format(%9.2f)) ///
		  graphregion(color(white)) scheme(sj) 
graph export "$tap_replication_output/FigureC1_Paraguay_OutOfWedlock_Muni.png", replace width(3900)
// graph export "$graph_folder\Paraguay_CompareData_Illegit_CstYear_Muni.png", replace width(3900)

* ---- At the Country Level	  
use "$tap_replication_data/Paraguay_ComAncestryCesus_IllegitCYear_Nation_restrict.dta", clear 
//use "$triplealliance_data_analyze\Paraguay_ComAncestryCesus_IllegitCYear_Nation.dta", clear 

graph bar (mean) birth_ratio_illegit (mean) birth_ratio_illegit_census, over(cst_year, label(angle(90) labsize(tiny))) ///
		  title("At the Country Level") ylabel(, labsize(*.8)) graphregion(color(white)) note("") ///
		  bar(1, color(black*0.7)) bar(2, color(gray*0.5)) legend(label(1 "Ancestry") label(2 "Census") size(tiny) region(lstyle(none))) 
graph export "$tap_replication_output/FigureC1_Paraguay_OutOfWedlock_Nation.png", replace width(3900)
//graph export "$graph_folder\Paraguay_CompareData_Illegit_CstYear_Nation.png", replace width(3900)

*   =====================================
*   Appendix Figure D.1: Gender Ratios in Modern Paraguay: 1950-2002.
*   =====================================

use "$tap_replication_data/Paraguay_Modern_GenderRatio_restrict.dta", replace
//use "$triplealliance_data_analyze\Paraguay_Modern_GenderRatio.dta", replace

* draw figure
graph bar (mean) Women (mean) Men, over(Year) stack ///
      ylabel( , labsize(*.8)) bar(1, color(red*0.2)) bar(2, color(blue*0.2)) blabel(bar, position(center) format(%9.2f)) ///
	  legend(label(1 "Women") label(2 "Men") rows(1) /*region(col(white))*/) graphregion(color(white))
	  
* add % sign into label	  
local nb=`.Graph.plotregion1.barlabels.arrnels'
forval i=1/`nb' {
  di "`.Graph.plotregion1.barlabels[`i'].text[1]'"
  .Graph.plotregion1.barlabels[`i'].text[1]="`.Graph.plotregion1.barlabels[`i'].text[1]'%"
}
.Graph.drawgraph	  

graph export "$tap_replication_output/FigureD1_Paraguay_Modern_SexRatio_CountryLevel.png", replace width(3900)	    
//graph export "$graph_folder\Paraguay_DS_Census_Modern_SexRatio_CountryLevel.png", replace width(3900)